home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpsehttp / wwwlib / amgr < prev    next >
Encoding:
Text File  |  1995-05-16  |  6.9 KB  |  301 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Archive manager for glimpseHTTP
  4.  
  5. # Location of httpd directory
  6. $HTTPD_HOME="/usr1/paul/httpd";
  7. $wwwlib = $HTTPD_HOME . "/wwwlib" ;
  8.  
  9. # Location of glimpse and glimpseindex, options for glimpseindex
  10. $GLIMPSE_LOC="/usr/local/bin/glimpse";
  11. $GLIMPSEIDX_LOC="/usr/local/bin/glimpseindex";
  12.  
  13. # server administrator name
  14. $adminname="Paul Klark";
  15. # server administrator e-mail
  16. $adminaddress="paul@cs.arizona.edu";
  17.  
  18. # index template
  19. $TEMPLATE="$wwwlib/template.bd";
  20. # description file, we try to find it in every directory
  21. $DESCFILE=".description";
  22. # name of the index file
  23. $HTMLINDEX="ghindex.html";
  24.  
  25. #
  26. # no configuration is needed below this line
  27. #
  28.  
  29. $config = "$wwwlib/amgr.cfg";
  30. $narch = 0;
  31. open(CFG,"<$config") ;
  32. $defserver = "";
  33. $next = "00";
  34. while (<CFG>) {
  35.     chop;
  36.     ($path[$narch],$url[$narch],$title[$narch],
  37.         $script[$narch],$server[$narch],$idxf[$narch],$idxn[$narch])
  38.             = split("\t");
  39.     $defserver = $server[$narch];
  40.     $next = $script[$narch];
  41.     $narch++;
  42. }
  43. $deftemplate = "template.bd";
  44. $defprogram = "aglimpse";
  45. close(CFG);
  46. &a_list;
  47. while ("true") {
  48.     print "(H)elp (E)dit (A)dd (D)elete (P)rint (L)ist (I)ndex (Q)uit\n" ;
  49.     $_ = &read("Enter command","Q");
  50.     /^[qQ]/ && last;
  51.     /^$/ && next;
  52.     ($command,$num) = split;
  53.     if (/^[pedi]/) {
  54.         if ($num !~ /^\d+/) {
  55.             print "This command requires ",
  56.                 "a numeric parameter - archive number (0...$#path)\n";
  57.             next;
  58.         }
  59.         if ( $num<0 || $num>$#path ) {
  60.             print "Archive number must be an integer ".
  61.                 "between 0 and $#path\n";
  62.             next;
  63.         }
  64.     }
  65.     if (/^[aA]/) {
  66.         &a_add;
  67.     } elsif (/^e/oi) {
  68.         &a_edit($num);
  69.     } elsif (/^d/oi) {
  70.         &a_delete($num);
  71.     } elsif (/^p/oi) {
  72.         &a_print($num);
  73.     } elsif (/^i/oi) {
  74.         &a_index($num);
  75.     } elsif (/^h/oi) {
  76.          &a_help;
  77.     } elsif (/^l/oi) {
  78.          &a_list;
  79.     } else {
  80.         print "Unknown command\n";
  81.     }
  82. }
  83. # now save the configuration
  84. open(CFG,">$config") ;
  85. for $i (0..$#path) {
  86.     print CFG $path[$i],"\t",$url[$i],"\t",$title[$i],"\t",
  87.       $script[$i],"\t",$server[$i],"\t",$idxf[$i],"\t",$idxn[$i],"\n";
  88. }
  89.  
  90. sub read {
  91.     local($prompt,$def) = @_;
  92.     print $prompt,"[",$def,"]:";
  93.     $| = 1;
  94.     $_ = <STDIN>;
  95.     chop;
  96.     return $_?$_:$def;
  97. }
  98. sub a_list {
  99.     local($i);
  100.     for $i (0..$#path) {
  101.         print "-" x20,"Archive #",$i,"\n";
  102.         &a_print($i);
  103.     }
  104. }
  105. sub a_print {
  106.     local($i) = $_[0];
  107.     print "Disk path     :",$path[$i],"\n";
  108.     print "URL path      :",$url[$i],"\n";
  109.     print "Title         :",$title[$i],"\n";
  110.     print "Script        :",$script[$i],"\n";
  111.     print "Server        :",$server[$i],"\n";
  112.     print "File per block:",$idxf[$i],"\n";
  113.     print "Index numbers :",$idxn[$i],"\n";
  114. }
  115. sub a_edit {
  116.     local($i) =    $_[0];
  117.     local($cwd);
  118.     $cwd = `pwd`;
  119.     chop $cwd;
  120.     do {
  121.         $path[$i] =    &read("Disk path     ",$path[$i]);
  122.         $result = chdir $path[$i];
  123.         if ($result) {
  124.             $path[$i] = `pwd`;
  125.             chop $path[$i];
  126.         } else {
  127.             print "Cannot chdir to $path[$i]: $!\n" unless $result;
  128.         }
  129.         chdir $cwd || die "Cannot chdir back to $cwd: $!\n";
  130.     } until $result;
  131.     $url[$i] =    &read("URL path      ",$url[$i]);
  132.     $title[$i] =    &read("Title         ",$title[$i]);
  133.     $script[$i] =    &read("Script        ",$script[$i]);
  134.     $server[$i] =    &read("Server        ",$server[$i]);
  135.     $idxf[$i] =    &read("File per block",$idxf[$i]);
  136.     $idxn[$i] =    &read("Index numbers ",$idxn[$i]);
  137. }
  138. sub a_add {
  139.     $#path++;
  140.     $#url++;
  141.     $#title++;
  142.     $#script++;
  143.     $script[$#script] = ++$next;
  144.     $#server++;
  145.     $server[$#server] = $defserver;
  146.     $#idxf++;
  147.     $idxf[$#idxf] = "yes";
  148.     $#idxn++;
  149.     $idxn[$#idxn] = "no";
  150.     &a_edit($#server);
  151.     $defserver = $server[$#server];
  152. }
  153. sub a_help {
  154.     system("more $wwwlib/README.amgr");
  155. }
  156. sub a_delete {
  157.     local($i) = @_;
  158.     splice(@path,$i,1);
  159.     splice(@url,$i,1);
  160.     splice(@title,$i,1);
  161.     splice(@script,$i,1);
  162.     splice(@server,$i,1);
  163.     splice(@idxf,$i,1);
  164.     splice(@idxn,$i,1);
  165. }
  166. sub a_index {
  167.     local($i) = @_;
  168.     local($path,$title);
  169.     $searchurl = $server[$i] . "/cgi-bin/".
  170.         $defprogram . "/" . $script[$i] ;
  171.     $iniurl = $server[$i] . "/" . $url[$i] ;
  172.     $path = $path[$i];
  173.     $title = $title[$i];
  174.     if ( $idxf[$i] =~ /^[yY]/ ) {
  175.         $GLIMPSEIDX_OPT = $GLIMPSEIDX_OPT . " -o";
  176.     }
  177.     if ( $idxn[$i] =~ /^[yY]/ ) {
  178.         $GLIMPSEIDX_OPT = $GLIMPSEIDX_OPT . " -n";
  179.     }
  180.     $| = 1;
  181.     chdir $path || die "Cannot chdir to $path to build indices: $!\n";
  182.     open(TEMPLATE,$TEMPLATE) || die "Cannot open index template file ".
  183.         "$TEMPLATE: $!\n";
  184.     $outid = "FILE000";
  185.     &do_bd("",$title);
  186.     close(TEMPLATE);
  187.     print "Do you want to rebuild Glimpse index as well [no]?";
  188.     $_ = <STDIN>;
  189.     if ( /^[yY]/ ) {
  190.         # unlink <.glimpse_[^i]*>;
  191.         $cmd = "$GLIMPSEIDX_LOC $GLIMPSEIDX_OPT -H . .";
  192.         system($cmd) && die "Cannot execute '$cmd': $!\n";
  193.     }
  194. }
  195.  
  196. sub do_bd {
  197.     local($relpath,$title) = @_;
  198.     local($file,$thisdir,$desc,$dirfiles,$dirsize,$totfiles,$totsize);
  199.     local(%desc,$header,$OUT,$DIR,$fileshere,$dirshere);
  200.     $totfiles = 0;
  201.     $totsize = 0;
  202.     $thisdir = `pwd`;
  203.     chop $thisdir;
  204.     undef $dirshere;
  205.     undef $fileshere;
  206.     
  207.     $OUT = $outid;
  208.     $DIR = $outid;
  209.     $outid++;
  210.     open($OUT,">$HTMLINDEX") || die "Cannot open file ".
  211.         "$thisdir/$HTMLINDEX for writing: $!\n";
  212.     print $OUT "\<HEAD>\<TITLE>$title\</TITLE></HEAD>\n";
  213.     print $OUT "\<BODY>\<H1>$title\</H1>\n";
  214.     &put_segment($OUT,"PROLOG");
  215.     # slurp description file into array desc
  216.     if (open(DESC,$DESCFILE)) {
  217.         descline: while (<DESC>) {
  218.             # print verbatim any lines beginning with @
  219.             if (s/^\@//) {
  220.                 print;
  221.                 next descline;
  222.             }
  223.             chop;
  224.             ($file,$desc) = split(/\t+/);
  225.             next descline unless $file =~ /^\w/;
  226.             $desc{$file} = $desc;
  227.         }
  228.         close(DESC);
  229.     }
  230.     opendir($DIR,".");
  231.     file: while ($file=readdir($DIR)) {
  232.         next if $file =~ /^\./;
  233.         next if $file eq $HTMLINDEX;
  234.         local($dev,$ino,$mode,$nlinsk,$uid,$gid,$rdev,$size) =
  235.             stat($file);
  236.         if (-d $file) {
  237.             print "subdirectory $path/$file\n";
  238.             chdir $file || next file;
  239.             $desc = $desc{$file};
  240.             if ($desc) {
  241.                 $header = $desc;
  242.                 ($dirfiles,$dirsize) =
  243.                     &do_bd("$relpath/$file",$desc);
  244.             } else {
  245.                 # no description - make it up
  246.                 $desc = "$title/$file";
  247.                 ($dirfiles,$dirsize) =
  248.                     &do_bd("$relpath/$file",$desc);
  249.                 $header = "Directory '$file' (";
  250.                 $header .= "$dirfiles files, " if $dirfiles;
  251.                 $header .= "total $dirsize bytes)";
  252.             }
  253.             $totfiles += $dirfiles;
  254.             $totsize += $dirsize;
  255.             chdir $thisdir;
  256.             $dirshere .= "<li><a href=\"".
  257.                 "$file/$HTMLINDEX\">$header</a>\n";
  258.         } else {
  259.             $totfiles += 1;
  260.             $totsize += $size;
  261.             $header = $desc{$file};
  262.             $header = "$file ($size bytes)" unless $header;
  263.             $fileshere .= "<li><a href=\"$file\">".
  264.                 "$header</a>\n";
  265.         }
  266.     }
  267.     closedir($DIR);
  268.     if ($dirshere) {
  269.         &put_segment($OUT,"PREDIR");
  270.         print $OUT $dirshere;
  271.         &put_segment($OUT,"POSTDIR");
  272.     }
  273.     &put_segment($OUT,"FORM");
  274.     if ($fileshere) {
  275.         &put_segment($OUT,"PREFILE");
  276.         print $OUT $fileshere;
  277.         &put_segment($OUT,"POSTFILE");
  278.     }
  279.     &put_segment($OUT,"EPILOG");
  280.     close($OUT);
  281.     return ($totfiles+1,$totsize);
  282. }
  283.  
  284. sub put_segment {
  285.     local($OUT,$label) = @_;
  286.     local($_,$flag);
  287.     seek(TEMPLATE,0,0);
  288.     $flag = 0;
  289.     line: while (<TEMPLATE>) {
  290.         next if /^\#/;
  291.         if (/^\@$label/) {
  292.             $flag = 1;
  293.             next line;
  294.         }
  295.         last line if $flag && /^\@/;
  296.         next line unless $flag;
  297.         s/\$\w[\w_]*/$&/gee;
  298.         print $OUT $_;
  299.     }
  300. }
  301.